package com.zhl.controller.meeting;

import com.zhl.controller.BaseServlet;
import com.zhl.model.Dept;
import com.zhl.model.Meeting;
import com.zhl.model.User;
import com.zhl.service.DeptService;
import com.zhl.service.MeetingService;
import com.zhl.service.UserService;
import com.zhl.service.impl.DeptServiceImpl;
import com.zhl.service.impl.MeetingServiceImpl;
import com.zhl.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/meeting/*")
public class meetingServlet extends BaseServlet {

    DeptService deptService = new DeptServiceImpl();
    MeetingService meetingService = new MeetingServiceImpl();
    UserService userService = new UserServiceImpl();

    public void findMeeting(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String title = req.getParameter("title");
        String status = req.getParameter("dep");
        List<Meeting> meetings = meetingService.findMeetings(title,status);
        req.getSession().setAttribute("meetings",meetings);
        req.getRequestDispatcher("/html/meeting.jsp").forward(req,resp);
    }

    //会议详情
    public void meetingDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String meetingId = req.getParameter("meetingId");
        Map<String, Object> meetingById = meetingService.findMeetingById(Integer.parseInt(meetingId));

        User user = (User)req.getSession().getAttribute("userById");
        //true表示已打卡，false表示未打卡
        boolean joinById = meetingService.findJoinById(user.getId(), Integer.parseInt(meetingId));
        req.getSession().setAttribute("joinById",joinById);

        req.getSession().setAttribute("meetingById",meetingById);
        req.getRequestDispatcher("/html/meeting_detail.jsp").forward(req,resp);
    }

    //更新会议打卡信息
    public void updateJoin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String meetingId = req.getParameter("meetingId");
        User user = (User)req.getSession().getAttribute("userById");
        Integer a = meetingService.updateJoinById(user.getId(), Integer.parseInt(meetingId));
        //返回a,2:该会议没有当前用户名字,1:参加会议失败，0:参加会议成功
        resp.getWriter().write(String.valueOf(a));
    }

    //转发到jsp的视图接口
    public void addMeeting(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        List<Dept> depts = deptService.findDepts();
        req.getSession().setAttribute("depts",depts);
        req.getRequestDispatcher("/html/meeting_add.jsp").forward(req,resp);
    }

    //关联下拉框
    public void updateSelect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.getSession().removeAttribute("usersBydId");
        List<User> usersBydId = null;
        String deptId = req.getParameter("deptId");
        if (deptId != null){
            usersBydId = userService.findUserByDeptId(Integer.parseInt(deptId));
        }
        req.getSession().setAttribute("usersBydId",usersBydId);
    }

    //添加会议接口
    public void saveMeeting(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String title = req.getParameter("title");
        String deptId = req.getParameter("deptId");
        String make_user = req.getParameter("arr");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        String content = req.getParameter("content");

        Meeting meeting = new Meeting();
        meeting.setTitle(title);
        Dept dept = deptService.findDeptById(Integer.parseInt(deptId));
        meeting.setDeptId(dept.getId());
        meeting.setDeptName(dept.getName());
        meeting.setMakeUser(make_user);

        Date startDate = null;
        Date endDate = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            startDate = formatter.parse(startTime);
            endDate = formatter.parse(endTime);
        }catch (Exception e){
            e.printStackTrace();
        }
        meeting.setStartTime(startDate);
        meeting.setEndTime(endDate);
        meeting.setContent(content);
        meeting.setPublishDate(new Date());
        meeting.setStatus(0);
        boolean b = meetingService.saveMeeting(meeting);
        if (b){
            resp.getWriter().write("0");
        }else {
            resp.getWriter().write("1");
        }
    }
}
